namespace Common
{
    using System;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class MessageBox
    {
        private MessageBox()
        {
        }

        public static void ResponseScript(Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");
        }

        public static void Show(Page page, string msg)
        {
            msg = msg.Replace("\r\n", "\\r\\n");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript'>$(document).ready(function(){ alert('" + msg + "');});</script>");
        }
        public static void SetFocus(Page page, Control webControl)
        {
            string controlName = webControl.ClientID;
            page.ClientScript.RegisterStartupScript(page.GetType(), "focus", "<script language='javascript'>IsInitialFocus=1;$(document).ready(function(){$('#" + controlName + "').focus();$('#" + controlName + "').select();});</script>");
        }
        public static void SetControlFocusSetControlFocus(Page page, ref Control focusControl, TextBox tempControl)
        {

            string id = tempControl.ID;
            if (id == null)
            {
            }
            id = "divMsg" + id;
            focusControl = focusControl ?? tempControl;
            page.ClientScript.RegisterStartupScript(page.GetType(), id, "<script language='javascript'>$(document).ready(function(){$('#" + id + "').attr('isError','true').attr('class','msgError');});</script>");

        }

        /// <summary>
        /// Shows the and redirect.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="msg">The MSG.</param>
        /// <param name="url">The URL.</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            msg = msg.Replace("\r\n", "\\r\\n");
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        /// <summary>
        /// Shows the and redirect.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="msg">The MSG.</param>
        /// <param name="url">The URL.</param>
        public static void Redirect(Page page, string url)
        {

            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }
        public static void ShowConfirm(WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }
    }
}

